//
//  ForthViewController.m
//  UIComprehensive
//
//  Created by lanou3g on 16/1/25.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "ForthViewController.h"
#import "StudentModel.h"
#import "GDataXMLNode.h"
#import "JSONKit.h"
#import "NetViewController.h"
@interface ForthViewController ()<NSXMLParserDelegate>

{
    NSMutableArray *arrAllData;
    NSMutableDictionary *dic;
    NSString *key;
    NSString *value;
    StudentModel *mode;
}

@end

@implementation ForthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"数据解析";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"网络解析" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    self.navigationItem.rightBarButtonItem = right;
}
- (void)rightAction:(UIBarButtonItem *)sender{
    
    [self.navigationController pushViewController:[NetViewController new] animated:YES];
}
#pragma mark - XML解析的第一种形式：SAX
- (IBAction)XML_SAX:(id)sender {
    //取到xml路径（data接收）
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students" ofType:@"xml"];
    //NSData处理数据
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //用NSXMLParser类解析XML数据
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    //设置代理
    parser.delegate = self;
#warning SAX启动解析
    [parser parse];
}
#warning SAX开始解析
- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"开始解析");
    //开始解析时，把存储数据的数组初始化
    arrAllData = [NSMutableArray array];
}
#warning SAX找到开始解析节点
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    NSLog(@"开始节点：%@",elementName);
    //每有一个student节点就创建一个字典
    if ([elementName isEqualToString:@"student"]){
        dic = [NSMutableDictionary dictionary];
        mode = [[StudentModel alloc]init];
        
    }
    //将节点作为key值
    key = elementName;
    
}
#warning SAX找到数据
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if(![string containsString:@"\n"]){
        //将数据作为value
        value = string;
    }
}
#warning SAX找到结束解析节点
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    //对应的键值对存到字典
    [dic setValue:value forKey:key];
    [mode setValuesForKeysWithDictionary:dic];
    
    if ([elementName isEqualToString:@"student"]) {
        [arrAllData addObject:mode];
    }
}
#warning SAX结束解析
-(void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"SAX解析结束");
    NSLog(@"SAX解析%@",arrAllData);
}

#pragma mark - XML解析的第二种形式：DOM
- (IBAction)XML_DOM:(id)sender {
    //利用GDataXMLNode封装好的库直接进行解析，代码简单，但解析前需要配置XCode
    /*
     NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students" ofType:@"xml"];
     NSData *data = [NSData dataWithContentsOfFile:filePath];
     
     GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
     //拿到根节点
     GDataXMLElement *element = [document rootElement];
     //找到我们想要的student节点
     NSArray *array = [element elementsForName:@"student"];
     //遍历拿到的所有student数据
     for (GDataXMLElement *ele in array) {
     //拿到姓名
     GDataXMLElement *name1 = [[ele elementsForName:@"name"] lastObject];
     NSString *name = [name1 stringValue];
     //
     GDataXMLElement *gender1 = [ele elementsForName:@"gender"].lastObject;
     NSString *gender = [gender1 stringValue];
     NSString *age =[[ ele elementsForName:@"age"].lastObject stringValue];
     
     StudentMode *mode1 = [[StudentMode alloc]init];
     mode1.name = name;
     mode1.gender = gender;
     mode1.age = age;
     }
     */

}
#pragma mark -JOSN解析的第一种形式：Foundation
- (IBAction)JSON_Foundation:(id)sender {
    
     //用data接收数据
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Students" ofType:@"txt"]];
    //解析
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@",array);
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dicTemp  in array) {
        StudentModel *model = [[StudentModel alloc]init];
        [model setValuesForKeysWithDictionary:dicTemp];
        [arr addObject:model];
    }
    NSLog(@"JOSN_Foundation%@",arr);
    
}
#pragma mark - JOSN解析的第二种形式：JsonKit
- (IBAction)JSONJSONKIT:(id)sender {
    //利用JSONKit第三方直接解析，但解析前有复杂的配置步骤
    /* */
    //用data接收数据
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Students" ofType:@"txt"]];
    //JSONKit直接解析
    NSArray *arr = [data objectFromJSONData];
    NSLog(@"JsonKit解析%@",arr);
    
}



@end
